%real time remove background
clc,clear
mode=2;
YearMonthDay=20190517;
addpath(genpath('D:\Rb87ProgramBEC3\removeBackground\'));
photoBEC='\\Camera-PC\';
if strcmp(photoBEC,'\\Camera-PC\')
    photoNamePre='4550test_';
    filename_pre='ascOD4550test_';
    resolution=512;
    folderName=[photoBEC 'ShareFolder\' num2str(YearMonthDay) 'morning\4550\'];
elseif strcmp(photoBEC,'\\BEC-DELL-PC\')
    photoNamePre='17639test_';
    filename_pre='ascOD17639test_';
    resolution=1024;
    folderName=[photoBEC 'ShareFolder\' num2str(YearMonthDay) 'morning\ikon-17639\'];
end
saveFolder=['D:\oriData\' num2str(YearMonthDay) '\'];
mkdir(saveFolder);
refimagesIniDirectory='D:\Rb87files\refimagesLibrary\refimagesIni4550.mat';
figuremode='1picture';
drawSwitch=0;

%initial------------------------------------------------------------
overWrite=0;
refNum=250;
checkCycle=5;%second
%��������������������������������������������������������������������������������
photoStart=0;
%��������������������������������������������������������������������������������

bgmask2=ones(resolution,resolution);
bgmask3=-ones(resolution,resolution);
bgmask1=zeros(resolution,resolution);
% rectangle_FWHM=150;

%--------------------------------------------------------------------------
%find center of atoms
% initial radial of region of cloud
% radial0=70;
%edge value of atom cloud:edge0(1),find edge;edge0(2),define edge and final region of atoms cloud
% edge0=[0.05 0.05];
%number of cloud in a picture
% cloudofnumber=2;
%scan region
% x_initial=80;
% y_initial=80;
% x_final=800;
% y_final=800;
%adjacent scan displacement
% scan_displacement=10;
%distance between scanning circle and maxium of inside circle
% distance=2;
%--------------------------------------------------------------------------

xminROI1=25;yminROI1=70;
xmin=150;ymin=110;rectangle_width=270;rectangle_height=350;
%change ROI and ROI1 by manual
ROI=[xmin,ymin,rectangle_width,rectangle_height];
ROI1=[xminROI1-10,yminROI1-10,370,370];
ROIX_vector=ROI(1):(ROI(1)+ROI(3));
ROIY_vector=ROI(2):(ROI(2)+ROI(4));
ROIX_vector1=ROI1(1):(ROI1(1)+ROI1(3));
ROIY_vector1=ROI1(2):(ROI1(2)+ROI1(4));
bgmask3(ROIY_vector,ROIX_vector)=-2;
bgmask1(ROIY_vector1,ROIX_vector1)=1;
bgmask=bgmask1+bgmask2+bgmask3;
% refimagesIni(:,:,210)=refCreate(13,'bg','E:\�׳���\Rb87Files\20161205morning\ikon_17639\','17639test_',1024);
%load reference images
refimagesIniStruct=load(refimagesIniDirectory);
refimagesIni=refimagesIniStruct.refimagesIni;

if size(refimagesIni,3)==refNum
    refimages=refimagesIni;
else
    error('Error: refNum ~= loaded reference images');
end
if mode==2
    cd(folderName);
    newestDatenum=0;
    photoInd=photoStart;
    
    while(true)
        savefilename= photoName(saveFolder,photoNamePre,photoInd,'od.mat');
        if exist(photoName(folderName,photoNamePre,photoInd,'nolight.asc'),'file')
            try
%             if exist(photoName(folderName,photoNamePre,photoInd,'.asc'),'file')
                files_if=[photoInd photoInd];
                [image]=drawpicture(folderName,filename_pre,files_if,figuremode);
                if drawSwitch==1
                    figure(1);imagesc(image(1).data,[-0.1,0.5]);title(num2str(photoInd));
                    rectangle('position',ROI);
                    colormap(jet);
                end
%                 figure(10),imagesc(bgmask);
%             end
            if overWrite==0 && exist(photoName(folderName,photoNamePre,photoInd,'od.mat'),'file')==2
                disp([photoName(folderName,photoNamePre,photoInd,'od.mat') ' already exist, skip it']);
                photoInd=photoInd+1;
                continue;
            end
            absimages=loadPhoto(photoName(folderName,photoNamePre,photoInd,'.asc'));
            refimages(:,:,mod(photoInd,refNum)+1)=loadPhoto(photoName(folderName,photoNamePre,photoInd,'bg.asc'));
            nolimage=loadPhoto(photoName(folderName,photoNamePre,photoInd,'nolight.asc'));
            
            %calculate
            refimageCal=fringeremoval(absimages,refimages,bgmask);
            refimageCalMNolight=((refimageCal-nolimage)+abs(refimageCal-nolimage))/2+1;
            absimageMNolight=((absimages-nolimage)+abs(absimages-nolimage))/2+1;
            odimage=log(  refimageCalMNolight./absimageMNolight  );
            
            if drawSwitch==1
                figure(2);
                imagesc(odimage,[-0.1,0.5]);
                title(photoName(folderName,photoNamePre,photoInd,''));
                rectangle('position',ROI);
                colormap(jet);
            end
                       
            savefilename= photoName(saveFolder,photoNamePre,photoInd,'od.mat');
            save(savefilename,'odimage');
           refimagesIni=refimages;
            save(refimagesIniDirectory,'refimagesIni');
            photoInd=photoInd+1;
            catch ME
                pause(0.1)
%                 disp(ME.identifier);
%                 [y Fs] = audioread('rmbg.mp3');
%                 sound(y,Fs);
            end
        else
            
            pause(checkCycle);
        end
    end
end